//
//  queRenViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2019/1/25.
//  Copyright © 2019 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class queRenViewController: UIViewController {
    var tableView  : UITableView!
    
    var deploymentName:String!
    
    var namestring:String!
    
    var photostring:String!

    var banlirenid:String!
    
    
    
    var querentuisong:String!
    
    var querenPhotot:String!
    
    var querentuisongID:String!
    
    
    
    var tuisong:String!
    
    var chuli:String!
    
    var luruButton:UIButton!
    
    
    var linkmans:String!
    
    var telphones:String!
    

    
    
    
    
    
    
    var agreementAmount:String?
    var areaCovered:String?
    var buildApprove:String?
    var buildApproveRemark:String?
    var buildCovered:String?
    var businessRegist:String?
    var businessRegistRemark:String?
    var buttSituation:String?
    var caFuName:String?
    var caName:String?
    var capitalAttractor:String?
    var compAccepReport:Array<Any>?
    var completePercent:String?
    var completeWorkPercent:String?
    var coopePhone:String?
    var createtime:String?
    var departmentName:String?
    var employNum:String?
    var environmentApprove:String?
    var environmentApproveRemark:String?
    var existCapital:String?
    var existCapitalProve:Array<Any>?
    var fixedAssetExistCapital:String?
    var fixedAssetYearExistCapital:String?
    var ganOutVal:String?
    var impLevel:String?
    var inChina:String?
    var industryCategoryId:String?
    var investmentBook:Array<Any>?
    var investLevel:String?
    
    var investorAddressCity:String?
    var investorAddressCityId:String?
    var investorAddressCountry:String?
    
    var investorAddressCountryId:String?
    
    var investorAddressProvince:String?
    var investorAddressProvinceId:String?
    
    var investorName:String?
    var landApprove:String?
    var landApproveRemark:String?
    var linkManArray:Array<Any>?
    var linkName:String?
    var newsReport:Array<Any>?
    
    
    var obtainNum:String?
    var outChinaAddress:String?
    var outputValue:String?
    var performTime:String?
    var planApprove:String?
    var planApproveRemark:String?
    
    var productOfflinePhoto:Array<Any>?
    var projectAdvPlan:String?
    var projectApprove:String?
    var projectApproveRemark:String?
    var projectAreaProvince:String?
    var projectBuildAddr:String?
    var projectContent:String?
    var projectIntro:String?
    var projectLevel:String?
    
    
    var projectName:String?
    var projectNum:String?
    var projectRealPhoto:Array<Any>?
    var projectReferee:String?
    var projectRefereeTelphone:String?
    var projectSpeed:String?
    var projectStage:String?
    var putIntoProductTime:String?
    var questionDesp:String?
    
    var signPicture:Array<Any>?
    var signTime:String?
    var startLicence:Array<Any>?
    var startRite:Array<Any>?
    
    var startTime:String?
    var taxTotalVal:String?
    var topOfWorld:String?
    var unitAddress:String?
    var unitLeader:String?
    
    
    var unitName:String?
    
    var unitPropert:String?
    var yearExistCapital:String?
    var yearTaxTotal:String?
    
    
    
    
    
    
    var taskId:String?
    
    var exclusiveGateWayFlag:String?
    
    var informationRecordId:String?
    
    
    
    var countryLeaderMan:String?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        NotificationCenter.default.addObserver(self, selector: #selector(namephoto), name: NSNotification.Name(rawValue:"querenluru"), object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(querentuisongren), name: NSNotification.Name(rawValue:"querentuisong"), object: nil)


    }
    
    deinit {
        /// 移除通知
        NotificationCenter.default.removeObserver(self)
    }
    @objc func namephoto(nofi : Notification) {
        self.namestring = nofi.userInfo!["name"] as? String
        
        self.photostring = nofi.userInfo!["photo"] as? String
        
        self.banlirenid = nofi.userInfo!["id"] as? String
        
        
        
        self.tableView.reloadData()
    }
    
    
    @objc func querentuisongren(nofi : Notification) {
        self.querentuisong = nofi.userInfo!["name"] as? String
        
        self.querenPhotot = nofi.userInfo!["photo"] as? String
        
        self.querentuisongID = nofi.userInfo!["id"] as? String
        
        
      
        self.tableView.reloadData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = bgGrayColor;
        
        navigationItem.title="确认"
        tableView = UITableView(frame:self.view.frame, style:.plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        
        tableView.tableFooterView = UIView.init()
        
        
        tableView.register(NSClassFromString("banlirenTableViewCell"), forCellReuseIdentifier: "banlirenTableViewCell")
        
        
        
        tableView.register(NSClassFromString("xiangmujinduTableViewCell"), forCellReuseIdentifier: "xiangmujinduTableViewCell")
        
        self.view.addSubview(tableView)
        tableView.sd_layout()?.spaceToSuperView(UIEdgeInsets.zero)
        
        self.luruButton = UIButton()
        //背景颜色
        self.luruButton.frame = CGRect(x: 10, y: self.view.frame.size.height - 49 - 40 , width: self.view.frame.size.width - 20, height: 40)
        self.luruButton.backgroundColor = UIColor.blue
        self.luruButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        self.luruButton.setTitle("确认", for: .normal )
        
        
        self.luruButton.addTarget(self,action:#selector(querenButtonClick), for: .touchUpInside)
        self.view.addSubview(self.luruButton)
        
        
        // Do any additional setup after loading the view.
    }
    
    @objc func querenButtonClick()  {
        
        
        let manager = UserDefaults()
        
        
        let token: String? = manager.string(forKey:"tokens")  //根据关键字取值
        

        
        if self.exclusiveGateWayFlag == nil {
            self.exclusiveGateWayFlag = "1"
        }else if self.exclusiveGateWayFlag == "可行"{
            self.exclusiveGateWayFlag = "1"
        }else{
            self.exclusiveGateWayFlag = "0"
        }
        
        
        if self.banlirenid == nil {
            self.banlirenid = ""
        }
        
        if  self.querentuisongID == nil {
            self.querentuisongID = ""
        }
        if self.tuisong == "推送" {
            self.tuisong = "1"
        }else{
            self.tuisong = "2"
        }
        
        if self.deploymentName == "项目流程" {
            
        }else if self.deploymentName == "问题单"||self.deploymentName == "问题单推送"{
            
        }else if self.deploymentName == "签约"||self.deploymentName == "履约"||self.deploymentName == "开工"||self.deploymentName == "投产"{
            
            let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/activiti/task/childCompleteTask"
            
            let json = "{\"token\":\"\(token!)\",\"taskId\":\"\(self.taskId!)\",\"exclusiveGateWayFlag\":\"\(self.exclusiveGateWayFlag!)\",\"informationRecordId\":\"\(self.informationRecordId!)\",\"countryLeader\":\"\(self.banlirenid!)\",\"pushMan\":\"\(self.querentuisongID!)\",\"propMove\":\"\(self.tuisong!)\",\"countryLeaderMan\":\"\(self.countryLeaderMan!)\",\"opinion\":\"\("1")\"}"
            
            //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
            print("json====\(json)")
            
            let url = URL(string: urlString)!
            let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
            
            var request = URLRequest(url: url)
            request.httpMethod = HTTPMethod.post.rawValue
            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
            request.httpBody = jsonData
            
            
            
            Alamofire.request(request).responseJSON {(response) in
                
                
                
                print(response)
                if response.result.isSuccess {
                
                    let json = JSON(response.data!)
                    
                    print("zhifu=====\(json["message"])")
    
                }else{
                   
                }
            }
            
        }
    }
 

}
extension queRenViewController: UITableViewDelegate, UITableViewDataSource {
    
    
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            return 2
        }else if section == 1{
            return 1
        }else{
            return 1
        }
    }
    

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        
        
        if section  == 0 {
            
            
            let headerView = UIView()
            headerView.backgroundColor = UIColor.white
            
            let titleLabel = UILabel()
            titleLabel.text = "选择"
            titleLabel.textColor = UIColor.gray
            titleLabel.sizeToFit()
            titleLabel.frame = CGRect(x: 10, y: 10, width:self.view.frame.size.width - 10, height: 20)
            headerView.addSubview(titleLabel)
            return headerView
        }else if section == 1 {
            
            
            
            let headerView = UIView()
            headerView.backgroundColor = UIColor.white
            let titleLabel = UILabel()
            titleLabel.text = "审批人"
            titleLabel.textColor = UIColor.gray
            titleLabel.sizeToFit()
            titleLabel.frame = CGRect(x: 10, y: 20, width:self.view.frame.size.width - 10, height: 20)
            headerView.addSubview(titleLabel)
            return headerView
        }else{
            let headerView = UIView()
            headerView.backgroundColor = UIColor.white
            let titleLabel = UILabel()
            titleLabel.text = "推送人"
            titleLabel.textColor = UIColor.gray
            titleLabel.sizeToFit()
            titleLabel.frame = CGRect(x: 10, y: 20, width:self.view.frame.size.width - 10, height: 20)
            headerView.addSubview(titleLabel)
            return headerView
        }
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0{
            return 40
        }else if indexPath.section == 1{
            return 60
        }else{
           
            
            if self.tuisong == "推送" {
                return 60
            }else{
                return 0
            }
        }
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if section == 0 {
            return 0
        }
        
        if section == 2 {
            if self.tuisong == "推送" {
                return 40
            }else{
                return 0
            }
        }else{
            return 40
        }
        
    }

    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            
            if indexPath.row == 0{
                let cell = xiangmujinduTableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "xiangmujinduTableViewCell")
                            cell.titleLbel.text = "处理结果"
                            if self.chuli == nil {
                                cell.nameLbel.text = ""
                            }else{
                                cell.nameLbel.text = self.chuli
                            }
                
                            cell.jinduButton.addTarget(self, action: #selector(chuliButtonClick), for: .touchUpInside)
                cell.selectionStyle = UITableViewCellSelectionStyle.none
                return cell
            }else{
                let cell = xiangmujinduTableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "xiangmujinduTableViewCell")
                            cell.titleLbel.text = "是否推送"
                            if self.tuisong == nil {
                                cell.nameLbel.text = ""
                            }else{
                                cell.nameLbel.text = self.tuisong
                            }
                
                            cell.jinduButton.addTarget(self, action: #selector(tuisongButtonClick), for: .touchUpInside)
                cell.selectionStyle = UITableViewCellSelectionStyle.none
                return cell
            }
           
        }else if indexPath.section == 1{
            let cell = banlirenTableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "banlirenTableViewCell")
            cell.banlirenButton.tag = 100
            
            cell.selectionStyle = UITableViewCellSelectionStyle.none

            
            if self.namestring == nil
            {
                cell.banlirenButton.addTarget(self, action: #selector(banlirenButtonClick(button:)), for: .touchUpInside)
                cell.nameLabel.isHidden = true
                
                
                
            }else
            {
                cell.banlirenButton.addTarget(self, action: #selector(banlirenButtonClick(button:)), for: .touchUpInside)
                cell.nameLabel.isHidden  = false
                cell.nameLabel.text = self.namestring!
                
                if self.photostring == ""
                {
                    cell.banlirenButton.setImage(UIImage.init(named: "morentouxiang"), for: .normal)
                }
                else
                {
                    let url : NSURL = NSURL(string: "http://wwwqjzsyzcscom.aykj.net.cn/api\(self.photostring!)")!
                    
                    let data : NSData = NSData(contentsOf:url as URL)!
                    let images = UIImage(data:data as Data, scale: 1.0)
                    
                    cell.banlirenButton.setImage(images!, for: .normal)
                }
                
            }
            return cell
        }else{
            let cell = banlirenTableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "banlirenTableViewCell")
            cell.banlirenButton.tag = 101
            
            cell.selectionStyle = UITableViewCellSelectionStyle.none
            
            if self.tuisong == "推送"{
                cell.isHidden = false
            }else{
                cell.isHidden = true
            }
            
            if self.querentuisong == nil
            {
                cell.banlirenButton.addTarget(self, action: #selector(banlirenButtonClick(button:)), for: .touchUpInside)
                cell.nameLabel.isHidden = true
                
                
                
            }else
            {
                cell.banlirenButton.addTarget(self, action: #selector(banlirenButtonClick(button:)), for: .touchUpInside)
                cell.nameLabel.isHidden  = false
                cell.nameLabel.text = self.querentuisong!
                
                if self.querenPhotot == ""
                {
                    cell.banlirenButton.setImage(UIImage.init(named: "morentouxiang"), for: .normal)
                }
                else
                {
                    let url : NSURL = NSURL(string: "http://wwwqjzsyzcscom.aykj.net.cn/api\(self.querenPhotot!)")!
                    
                    let data : NSData = NSData(contentsOf:url as URL)!
                    let images = UIImage(data:data as Data, scale: 1.0)
                    
                    cell.banlirenButton.setImage(images!, for: .normal)
                }
                
            }
            return cell
        }
        
        
        
        
        
        
    }
    @objc func tuisongButtonClick(){
        CGXPickerView.showStringPicker(withTitle: "是否推送", dataSource: ["推送","不推送"], defaultSelValue: nil, isAutoSelect: false, manager: nil) { (selectValue, selectRow) in
            
            print(selectValue!)
            
            
            self.tuisong = selectValue as? String
            
            //            let indexPath: IndexPath = IndexPath.init(row: 4, section: 7)
            
            self.tableView.reloadData()
            
        }
    }
    
    @objc func chuliButtonClick(){
        CGXPickerView.showStringPicker(withTitle: "处理结果", dataSource: ["同意","不同意"], defaultSelValue: nil, isAutoSelect: false, manager: nil) { (selectValue, selectRow) in
            
            print(selectValue!)
            
            
            self.chuli = selectValue as? String
            
            //            let indexPath: IndexPath = IndexPath.init(row: 4, section: 7)
            
            self.tableView.reloadData()
            
        }
    }
    
    
    
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
        
        
    }
    @objc func  banlirenButtonClick(button:UIButton) {
        
        if button.tag == 100 {
            
            print(button.tag)
            let banVC =  banlirenViewController()
            banVC.ButtonTag = button.tag
            banVC.xiangmu = "确认"
            banVC.zhiwei = "审批人"
            self.navigationController?.pushViewController(banVC, animated: true)
        }else{
            let banVC =  banlirenViewController()
            banVC.ButtonTag = button.tag
            banVC.xiangmu = "确认"
            banVC.zhiwei = "推送人"
            
            self.navigationController?.pushViewController(banVC, animated: true)
        }
        
    }
    
    
}
